package yjpay.index.web.control;

import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.util.encrypt.EncryptionUtil;

@Controller
public class IndexWebCtrl {

	@RequestMapping(value = "/")
	public String index(HttpServletResponse response, HttpServletRequest request) {
		return "index.jsp";
	}

	@RequestMapping(value = "/pay/home")
	public String home() {
		return "home.jsp";
	}

	/**
	 * 登陆
	 * 
	 * @param pwd
	 * @param model
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/login")
	public ModelAndView login(@RequestParam("pwd") String pwd, Model model,
			HttpServletResponse response, HttpServletRequest request) {

		ResourceBundle resb1 = ResourceBundle.getBundle("payapi");

		// 从配置文件读取易宝分配的公钥
		String password = resb1.getString("password");

		String encryptkey = EncryptionUtil.md5(pwd);

		ModelAndView modelAndView = new ModelAndView();
		if (password.equals(encryptkey)) {
			modelAndView.setViewName("home.jsp");
			// 设置session
			HttpSession session = request.getSession();
			session.setAttribute("yj_wappay", password);
			return modelAndView;
		} else {
			modelAndView.setViewName("index.jsp");
			modelAndView.addObject("info", "密码错误！");
			return modelAndView;
		}
	}

}
